import service from '../../services/choose';
import base from '../../config/base.js';
Page({
  data: {
    icon_nosearch: base.static + 'choose/nosearch.png',
    icon_search: base.static + 'choose/search3.png',
    icon_quanshan: base.static + 'choose/quanshan.png',
    goodsList:[],
    loadBloon: true,
    endBloon: true,
    pages: 0,
    noMore: true,
    noSearch: true,
    value: '',
    store_id: '',
    user_id: '',
    listBloon: false,
    clickBloon: true,
    scrolltop: '', //滚动条高度
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    let user_info = wx.getStorageSync('user_info') || {id:'',lastView:{seid:''}};
    that.setData({
      user_id: user_info.id,
      store_id: user_info.lastView.seid
    })
    //搜索历史
    that.searchHistory();
    //热门搜索
    that.hotSearch();

  },
  //搜索历史
  searchHistory : function(){
    let that = this;
    let params = {};
    service.customerSearchhistory(params,function(res){
      if(res && res.code == 1) {
        that.setData({
          historyList : res.data
        })
      }
    })
  },
  //热门搜索
  hotSearch: function () {
    let that = this;
    let params = {};
    service.hotkeyword(params, function (res) {
      if (res && res.code == 1) {
        that.setData({
          hotList: res.data
        })
      }
    })
  },
  //点击搜索
  clickSearch: function(){
    let that = this;
    that.setData({
      listBloon: true,
      goodsList: [],
      loadBloon: true,
      pages: 0,
      endBloon: true,
      noMore: true,
      noSearch: true,
    })
    let val = that.data.value.trim();
    that.setData({
      value: val
    })
    if (that.data.value == ''){
      that.goodsSearch(that.data.value, 1);
      return;
    }
    let strArr = that.data.historyList;
    let inx = "one";
    let searchword = { keyword: that.data.value};
    for(var i = 0; i < strArr.length; i++){
      if (strArr[i].keyword == that.data.value){
        if(i == 0){
          inx = 'two';  
          break;
        }else {
          inx = i;
          searchword = { keyword: strArr[i].keyword };
          strArr.splice(i, 1);
          strArr.unshift(searchword);
          break;
        }
        
      }
    }
    if(inx == "one"){
      strArr.unshift(searchword);
    }
    that.setData({
      historyList: strArr
    })
    that.goodsSearch(that.data.value, 1);
    that.setData({
      clickBloon: false //事件节流
    })
  },
  //点击历史搜索
  clickhistory : function(e){
    let that = this;
    that.setData({
      value: e.currentTarget.dataset.value,
      index: e.currentTarget.dataset.index,
      listBloon: true
    })
    let inx = "one";
    let strArr = that.data.historyList;
    let searchword = { keyword: that.data.value };
    for (var i = 0; i < strArr.length; i++) {
      if (strArr[i].keyword == that.data.value) {
        if (i == 0) {
          inx = 'two';
          break;
        } else {
          inx = i;
          searchword = { keyword: strArr[i].keyword };
          strArr.splice(i, 1);
          strArr.unshift(searchword);
          break;
        }

      }
    }  
    if (inx == "one") {
      strArr.unshift(searchword);
    }
    that.setData({
      historyList: strArr
    })
    that.goodsSearch(that.data.value, 1);
   
  },
  //输入框输入值
  changeValue: function(e){
    let that = this;
    that.setData({
      value: e.detail.value
    })
    if (that.data.value == ''){
      that.setData({
        listBloon: false,
        goodsList: [],
        loadBloon: true,
        pages: 0,
        endBloon: true,
        noMore: true,
        noSearch: true,
      })
    }
  },
  //清除输入值
  deleteValue: function () {
    this.setData({
      value: '',
      listBloon: false,
      goodsList: [],
      loadBloon: true,
      pages: 0,
      endBloon: true,
      noMore: true,
      noSearch: true,
    })

  },
  //搜索列表
  goodsSearch: function (keyword,pages) {
    let that = this;
    if (!that.data.clickBloon){
      return
    }
    let params = {
      page: pages,
      per_page: 10,
      keyword: keyword,
    };
    service.customerSearch(params, function (res) {
      if (res && res.code == 1) {
        let glist = that.data.goodsList.concat(res.data.dataList);
        if (glist !=""){
          glist.forEach((item,index)=>{
            item.src = item.goods_pic.url
            item.goods_id = item.goods_pic.goods_id
          })
        }
        that.setData({
          goodsList: glist,
          pages: pages + 1,
          endBloon: true,
          clickBloon: true
        })
        if (res.data.dataCount == 0){
          that.setData({
            noSearch: false
          })
        }
        if (glist.length >= res.data.dataCount && glist.length != 0) {
          that.setData({
            loadBloon: false,
            noMore: false
          })
        }
      } else if (res && res.code == 400){
        that.setData({
          noSearch: false
        })
      }
      wx.stopPullDownRefresh();
    });
  },
  //返回顶部
  goscoltop: function () {
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 300
    })
  },
  //监听页面高度
  scrollTimer: null,
  onPageScroll: function (res) {
    let that = this;
    if (that.scrollTimer) { clearTimeout(that.scrollTimer) };
    that.scrollTimer = setTimeout(() => {
      that.setData({
        scrolltop: res.scrollTop
      })
    }, 500);
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this;
    if (that.data.loadBloon && that.data.endBloon) {
      that.setData({
        endBloon: false
      })
      that.goodsSearch(that.data.value, that.data.pages);
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
   
  }
})